package com.dut.bbs.activity;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import com.dut.bbs.R;
import com.dut.bbs.data.ParseHot;
import com.dut.bbs.extend.treeview.TreeElement;
import com.dut.bbs.extend.treeview.TreeView;
import com.dut.bbs.extend.treeview.TreeView.ItemClickListener;
import com.dut.bbs.extend.treeview.TreeViewAdapter;
import com.dut.bbs.util.URLConstant;

public class FavoriteActivity extends BaseActivity {
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.category);
		TreeView treeView = (TreeView) findViewById(R.id.tree_view);
		treeView.initData(this, getItemList());
		treeView.setItemClickCallBack(new ItemClickListener() {
			
			@Override
			public void onLoading() {
				getProgressDialog().setMessage("抓取页面中...");
				getProgressDialog().show();
			}
			
			@Override
			public void onLastLevelItemClick(int position, TreeViewAdapter adapter) {
//				Toast.makeText(getApplicationContext(), ((TreeElement)adapter.getItem(position)).getTitle(), 300).show();
				Intent in = new Intent(FavoriteActivity.this, ThreadActivity.class);
				Bundle bundle = new Bundle();
				String url = ((TreeElement)adapter.getItem(position)).getUrl();
				bundle.putString("url", url);
				bundle.putString("title", ((TreeElement)adapter.getItem(position)).getTitle());
				in.putExtras(bundle);
				startActivity(in);
			}
			
			@Override
			public void onSuccess() {
				getProgressDialog().dismiss();
			}
			
			@Override
			public void onError() {
				Toast.makeText(getApplicationContext(), "加载失败，请重试", 300).show();
				getProgressDialog().dismiss();
			}
			
			@Override
			public List<Map<String, String>> parseData(String url) throws Exception {
				return ParseHot.getHotTopic(url);
			}
		});
	}
	
	ArrayList<TreeElement> itemList;
	ArrayList<TreeElement> getItemList() {
		if (itemList == null) {
			itemList = new ArrayList<TreeElement>();
			int i = 0;
			for(Entry<String, String> item : URLConstant.map0.entrySet()) {
				TreeElement element = new TreeElement();
				element.setId(i + "011");
				element.setLevel(1);
				element.setTitle(item.getKey());
				element.setUrl(item.getValue());
				element.setHasChild(true);
				itemList.add(element);
				i++;
			}
		}
		return itemList;
	}
}
